﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfEffect.中奖刮刮卡擦除效果
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        PathGeometry gridGeometry = new PathGeometry();
        private bool isDown = false;

        double originArea;
        bool hasShowResult = false;
        public MainWindow()
        {
            InitializeComponent();
            RectangleGeometry rg = new RectangleGeometry();
            rg.Rect = new Rect(0, 0, 800, 260);
            gridGeometry = Geometry.Combine(gridGeometry, rg, GeometryCombineMode.Union, null);
            gridShadow.Clip = gridGeometry;

            originArea= gridGeometry.GetArea();
        }


        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDown)
            {
                EllipseGeometry rg = new EllipseGeometry();
                rg.Center = e.GetPosition(gridShadow);
                rg.RadiusX = 20;
                rg.RadiusY = 20;
                //排除几何图形
                gridGeometry = Geometry.Combine(gridGeometry, rg, GeometryCombineMode.Exclude, null);
                gridShadow.Clip = gridGeometry;

                var currentArea = gridGeometry.GetArea();
                if ((currentArea * 100 / originArea) < 50 && !hasShowResult)
                {
                    hasShowResult = true;
                    MessageBox.Show("恭喜中奖1个亿!");
                }
            }
        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isDown = true;
        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
        }
    }
}
